//
//  InspirationSonViewController.m
//  PerfectWorld
//
//  Created by dllo on 16/9/20.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "InspirationSonViewController.h"
#import "AFNetworking.h"
#import "ideabooks.h"
#import "IdeaPhotos.h"
#import "SonUser.h"
#import "InspirationSonCollectionViewCell.h"
#import "InspirationSonLabelCollectionViewCell.h"
@interface InspirationSonViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)ideabooks *ideaBooks;
@end

@implementation InspirationSonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self handData];
    [self creatCollectionView];
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(20, 30, 50, 30);
    [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self.view addSubview:leftBtn];
    [leftBtn addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)handData {
    NSString *urlID = [NSString stringWithFormat:@"http://api.guju.com.cn/v2/ideabook/%@?user=(null)", _urlStr];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlID parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
         self.ideaBooks = [ideabooks modelWithDic:dic];
        [_collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
}
- (void)creatCollectionView {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.bounds.size.width / 3 - 5, 120);
    layout.minimumInteritemSpacing = 5;
    layout.minimumLineSpacing = 5;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.directionalLockEnabled = YES;
    self.collectionView.alwaysBounceVertical = YES;
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"InspirationSonCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
    [_collectionView registerNib:[UINib nibWithNibName:@"InspirationSonLabelCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"labelCell"];
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return _ideaBooks.photos.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height / 4);
    }
    return CGSizeMake(self.view.bounds.size.width / 3 - 5, 120);
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        InspirationSonLabelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"labelCell" forIndexPath:indexPath];
        if (_ideaBooks.I_description == nil) {
            return cell;
        } else {
        cell.label.text = _ideaBooks.I_description;
        cell.label.numberOfLines = 0;
        return cell;
        }
    } else {
    InspirationSonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.photos = _ideaBooks.photos[indexPath.row];
    return cell;
}
 }
- (void)clickLeft:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
